package com.orangelife.common;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.apache.commons.httpclient.HttpStatus;

import com.orangelife.framework.response.OLResponseSupport;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;

public class ResponseSupportSubLogic extends OLResponseSupport {

	/**
	 * 错误对象返回
	 * 
	 * @param errCode
	 * @param errInfo
	 * @return response
	 */
	public Response abnormalResp(Integer errCode, String errInfo) {
		return super.abnormalResp(errCode, errInfo);
	}

	/**
	 * 错误对象返回
	 * 
	 * @param errCode
	 * @param errInfo
	 * @return response
	 */
	public Response abnormalResp(Integer errCode, Integer errInfo) {
		return super.abnormalResp(errCode, String.valueOf(errInfo));
	}

	public Response abnormalResp(Integer code, String errInfo, String param) {
		return super.abnormalResp(code,
				String.format(errInfo.replace("{}", "%s"), param));
	}

	public Response abnormalResp(Integer code, String errInfo, String param1,
			String param2) {
		return super.abnormalResp(code,
				String.format(errInfo.replace("{}", "%s"), param1, param2));
	}

	/**
	 * 正常对象返回
	 * 
	 * @param errCode
	 * @param errInfo
	 * @return response
	 */
	public Response normalResp() {
		return super.normalResp(CodeConst.NORMAL, InfoConst.NORMAL);
	}

	/**
	 * 正常对象返回
	 * 
	 * @param errCode
	 * @param errInfo
	 * @param entity
	 * @return response
	 */
	public Response normalResp(Object entity) {
		return super.normalResp(CodeConst.NORMAL, InfoConst.NORMAL, entity);
	}

	/**
	 * 正常列表对象返回
	 * 
	 * @param errCode
	 * @param errInfo
	 * @param pageNum
	 * @param pageSize
	 * @param pageEntity
	 * @return response
	 */
	public Response normalResp(Integer pageNum, Integer pageSize, Long total,
			List<?> pageEntity) {
		return super.normalResp(CodeConst.NORMAL, InfoConst.NORMAL, pageNum,
				pageSize, total, pageEntity);
	}

	public Response normalRespForMap(String keys, Object... objects) {
		Map<String, Object> m = new HashMap<String, Object>();
		String[] n = keys.split("###");

		for (int i = 0; i < n.length; i++) {
			m.put(n[i], objects[i]);
		}
		return super.normalResp(CodeConst.NORMAL, InfoConst.NORMAL, m);
	}

	public Response normalResp2(Object entity) {
		ResponseBuilder rb = Response.status(HttpStatus.SC_OK);
		rb.type("application/json;charset=UTF-8");
		rb.language(Locale.SIMPLIFIED_CHINESE);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("errInfo", InfoConst.NORMAL);
		m.put("errCode", CodeConst.NORMAL);
		m.put("entity", entity);
		rb.entity(m);
		return rb.build();
		// return super.normalResp(CodeConst.NORMAL, InfoConst.NORMAL, entity);
	}

	/**
	 * 正常列表对象返回
	 * 
	 * @param errCode
	 * @param errInfo
	 * @param pageNum
	 * @param pageSize
	 * @param pageEntity
	 * @return response
	 */
	public Response normalResp2(Integer pageNum, Integer pageSize, Long total,
			List<?> pageEntity) {

		ResponseBuilder rb = Response.status(HttpStatus.SC_OK);
		rb.type("application/json;charset=UTF-8");
		rb.language(Locale.SIMPLIFIED_CHINESE);

		Map<String, Object> m = new HashMap<String, Object>();
		m.put("errInfo", InfoConst.NORMAL);
		m.put("errCode", CodeConst.NORMAL);
		m.put("total", total);
		m.put("pageSize", pageSize);
		m.put("pageIndex", pageNum);
		m.put("pageEntity", pageEntity);
		rb.entity(m);
		return rb.build();

	}

}
